package com.soda.servlet;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.soda.bean.News;
import com.soda.bean.Notice;
import com.soda.dao.NoticeDao;
import com.soda.dao.impl.NewsDaoImpl;
import com.soda.dao.impl.NoticeDaoImpl;
import com.soda.service.NewsService;
import com.soda.service.NoticeService;
import com.soda.service.impl.NewsServiceImpl;
import com.soda.service.impl.NoticeServiceImpl;

import com.soda.util.PageInfo;
import com.soda.util.SelectNewsInfo;
import com.soda.util.SelectNoticeInfo;

public class NoticeServlet extends HttpServlet {

	NoticeService nse = new NoticeServiceImpl();
	NoticeDao nd=new NoticeDaoImpl();
	SelectNoticeInfo sni = new SelectNoticeInfo();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String param = request.getParameter("param");// 获得参数，由参数判断执行方法
		if (param != null) {
			// 查看
			if ("Notice".equals(param)) {
				if(request.getParameter("aaa")!=null){
					request.getSession().removeAttribute("sni");
				}
				Notice(request, response);
			}
			// 删除
			if ("delete".equals(param)) {

				try {
					delete(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// 编辑内容
			if ("edit".equals(param)) {

				edit(request, response);
			}
			if ("update".equals(param)) {
				update(request, response);
			}
			// 增加
			if ("add".equals(param)) {
				
				add(request, response);
			}
			// 根据ID全查
						if ("showAllNotice".equals(param)) {
							showAllNotice(request, response);
						}
			// 批量删除
			if ("alldelete".equals(param)) {
				alldelete(request, response);
			}
			//导出
			 
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	// 增加
	protected void add(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("titleName");
		String Author = request.getParameter("Author");
		 
		int recommend = Integer.parseInt(request.getParameter("select2"));
		String neirong = request.getParameter("neirong");
		HttpSession session = request.getSession();

		Notice n = new Notice();
		n.setNotTitle(title);
		n.setNotUser(Author);
		n.setNotLevel(recommend);
		n.setNotContent(neirong);
		int num = nse.addNotice(n);
		if (num > 0) {

			try {
				Notice(request, response);
			response.sendRedirect("NoticeSerclet?param=Notice");
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 全查
	public void Notice(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		NoticeService nos = new NoticeServiceImpl();
		// 防止乱码

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
 
	
		//PageInfo pi = new PageInfo();
		PageInfo pi = new PageInfo(1, 5, 0, 0, "NoticeServlet", "Notice");
	
		// 从jsp中获取值
		String Title = request.getParameter("notTitle");
		String updateTime = request.getParameter("updateTime");
		String state = request.getParameter("select2");
		System.out.println(Title);
		System.out.println(updateTime);
		System.out.println(state);
		
		HttpSession session = request.getSession();
		if (session.getAttribute("sni") != null) {
			sni = (SelectNoticeInfo) session.getAttribute("sni");
		} else {
			sni = new SelectNoticeInfo("", "", -1);
		}

		if (Title != null) { // 分页条件赋值
			sni.setNotTitle(Title.length() == 0 ? "" : Title);

			sni.setNotUpdateTime(updateTime == null || updateTime.length() == 0 ? "":updateTime);
			sni.setNotLevel(state.length() == 0 || state == null ? -1 : Integer.parseInt(state));
			request.getSession().setAttribute("sni", sni);
		} else {
			sni = (SelectNoticeInfo) request.getSession().getAttribute("sni");
		}

		int page = 1;
		pi.setPer(5);// 分页信息赋值
		int count=nos.getNoticeCount(sni);
		pi.setTotal(count);
		pi.setLastPage((count+pi.getPer()-1)/pi.getPer());
		
		String pages = request.getParameter("page");
System.out.println(pi.getLastPage());
		if (pages != null) {
			pi.setPage(Integer.parseInt(pages));
		}

		List<Notice>list = nse.getNotices(sni, pi);
		System.out.println("size:"+list.size());
		request.setAttribute("notice", list);
		request.setAttribute("page", pi);
		session.setAttribute("state", state);
		request.getRequestDispatcher("houtai/pages/afficheManage/index.jsp").forward(request, response);

	}

	// 单个删除
	protected void delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception, IOException {
		response.setContentType("text/html;charset=utf-8");
		int Id = Integer.parseInt(request.getParameter("id"));

		if (nse.deleteById(Id) > 0) {
			Notice(request, response);
		}

	}

	// 批量删除
	protected void alldelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String dellist = request.getParameter("id");
		String []list=dellist.split(",");
		int num = nse.delAllNotice(list);
		PrintWriter out=response.getWriter();
		if (num > 0) {
			Notice(request, response);
	 
			}
	}

	// 根据ID查信息
	protected void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		
		int id = Integer.parseInt(request.getParameter("editId"));
		Notice notice = nse.showAllNotice(id);
		request.setAttribute("notice", notice);
		request.setAttribute("level", notice.getNotLevel());
		request.getRequestDispatcher("houtai/pages/afficheManage/edit.jsp").forward(request, response);

	}
	protected void showAllNotice(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		int id = Integer.parseInt(request.getParameter("Id"));
		Notice notice = nse.showAllNotice(id);
		request.setAttribute("notice", notice);
		request.setAttribute("level", notice.getNotLevel());
		request.getRequestDispatcher("houtai/pages/afficheManage/lookImpl.jsp").forward(request, response);

	
	
	}
	// 修改信息
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int id = Integer.parseInt(request.getParameter("id"));
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");

		int n = nse.updateNotice(id, recommend, neirong);

		if (n > 0) {
			Notice(request, response);

		} else {
			System.out.println("shibai");
		}
	}
	 

	
	
	
	
}